<?php

class Hoi_dong_thisController extends BaseController {

    /**
     * Hoi_dong_thi Repository
     *
     * @var Hoi_dong_thi
     */
    protected $hoi_dong_thi;

    public function __construct(Hoi_dong_thi $hoi_dong_thi) {
        $this->hoi_dong_thi = $hoi_dong_thi;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $hoi_dong_this = $this->hoi_dong_thi->all();

        return View::make('hoi_dong_this.index', compact('hoi_dong_this'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('hoi_dong_this.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Hoi_dong_thi::$rules);

        if ($validation->passes()) {
            $this->hoi_dong_thi->create($input);

            return Redirect::route('boards.index');
        }

        return Redirect::route('boards.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $hoi_dong_thi = $this->hoi_dong_thi->findOrFail($id);

        return View::make('hoi_dong_this.show', compact('hoi_dong_thi'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $hoi_dong_thi = $this->hoi_dong_thi->find($id);

        if (is_null($hoi_dong_thi)) {
            return Redirect::route('boards.index');
        }

        return View::make('hoi_dong_this.edit', compact('hoi_dong_thi'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $hoi_dong_thi = $this->hoi_dong_thi->findOrFail($id);
        $current_hoi_dong_thi = $hoi_dong_thi['ma_hoi_dong_thi'];
        $input = array_except(Input::all(), '_method');
        if($input['ma_hoi_dong_thi'] == $current_hoi_dong_thi){
            $hoi_dong_thi = $this->hoi_dong_thi->find($id);
            $hoi_dong_thi->update(
                array('ten_hoi_dong_thi'=>$input['ten_hoi_dong_thi'])
                );
            return Redirect::route('boards.show', $id);

        }else{
            
            $validation = Validator::make($input, Hoi_dong_thi::$rules);
            if ($validation->passes()) {
                $hoi_dong_thi = $this->hoi_dong_thi->find($id);
                $hoi_dong_thi->update($input);
                return Redirect::route('boards.show', $id);
            }     
        }



        

        if ($validation->passes()) {
            $hoi_dong_thi = $this->hoi_dong_thi->find($id);
            $hoi_dong_thi->update($input);

            return Redirect::route('boards.show', $id);
        }

        return Redirect::route('boards.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->hoi_dong_thi->find($id)->delete();
        return 1;
        // return Redirect::route('boards.index');
    }

}
